import {Injectable} from '@angular/core';
import {Router, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {CommonService} from './common.service';


@Injectable()
export class AuthGuard {

    constructor(private Service: CommonService, private router: Router) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot, Service: CommonService) {
        // 验证是否登录
        if (this.Service.getUserInfo()) {
            return true;
        } else {
            let ref = document.referrer;
            if(ref.indexOf('login') == -1 && ref.indexOf('register') == -1) {
                this.Service.setItem('ref',ref);
            }else{
                this.Service.setItem('ref','/');
            }
            location.href = '/user/login';
        }
    }
}
